package com.ytt.bargraph.huaweipaydemo;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.basedemos.R;

/**
 * Created by ytt on 2017/12/14.
 */

public class MyLinearLayout extends LinearLayout {
    private TextView     item_expand_textview;
    private TextView     item_expand;
    private ImageView    imageview;
    private LinearLayout item_expand_ll;
    //是否需要显示展开和折叠
    private boolean      isNeed;
    //是否展开
    private boolean      isExpand;
    private int          thisLineCount;

    private int maxLines = 3;

    public MyLinearLayout(Context context) {
        this(context, null);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_expand_child, null);
        LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        addView(view, params);
        item_expand_textview = (TextView) view.findViewById(R.id.item_expand_textview);
        item_expand = (TextView) view.findViewById(R.id.item_expand);
        imageview = (ImageView) view.findViewById(R.id.item_expand_imageview);
        item_expand_ll = (LinearLayout) view.findViewById(R.id.item_expand_ll);
        item_expand_ll.setOnClickListener(new MyTurnListener());
    }

    public void setText(String text, int childMaxLines, boolean isNeed) {
        this.isNeed = isNeed;
        this.maxLines = childMaxLines;
        item_expand_textview.setText(text == null ? "" : text);
        if (isNeed) {
            ViewTreeObserver vto = item_expand_textview.getViewTreeObserver();
            vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    int lineCount = item_expand_textview.getLineCount();
                    Log.i("ytp", "总共有几行:" + lineCount);
                    if (lineCount > thisLineCount) {
                        thisLineCount = lineCount;
                    }
                    if (lineCount > maxLines && item_expand_textview.getMaxLines() != thisLineCount) {
                        if (item_expand_ll.getVisibility() != View.VISIBLE) {
                            item_expand_ll.setVisibility(View.VISIBLE);
                        }
                        if (!isExpand) {
                            item_expand_textview.setMaxLines(maxLines);
                            //  item_expand_textview.setEllipsize(TextUtils.TruncateAt.END);
                        } else {
                            item_expand_textview.setMaxLines(thisLineCount);
                        }
                    } else {
                    }
                    return true;
                }
            });
        } else {
            item_expand_textview.setMaxLines(maxLines);
            item_expand_textview.setEllipsize(TextUtils.TruncateAt.END);
            item_expand_ll.setVisibility(View.GONE);
        }
    }

    private class MyTurnListener implements View.OnClickListener {
        // boolean isExpand;  //是否翻转
        @Override
        public void onClick(View v) {
            isExpand = !isExpand;
            item_expand_textview.clearAnimation();  //清除动画
            item_expand.clearAnimation();
            final int tempHight;
            final int startHight = item_expand_textview.getHeight();  //起始高度
            int durationMillis = 500;

            if (!isExpand) {
                /**
                 * 折叠效果，从长文折叠成短文
                 */
                tempHight = item_expand_textview.getLineHeight() * maxLines - startHight;
                //翻转icon的180度旋转动画
                RotateAnimation animation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                animation.setDuration(durationMillis);
                animation.setFillAfter(true);
                imageview.startAnimation(animation);
                item_expand.setText("展开");
            } else {
                /**
                 * 展开效果，从短文展开成长文
                 */
                tempHight = item_expand_textview.getLineHeight() * thisLineCount - startHight;
                //翻转icon的180度旋转动画
                RotateAnimation animation = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                animation.setDuration(durationMillis);
                animation.setFillAfter(true);
                imageview.startAnimation(animation);
                item_expand.setText("收起");
            }

            Animation animation = new Animation() {
                //interpolatedTime 为当前动画帧对应的相对时间，值总在0-1之间
                protected void applyTransformation(float interpolatedTime, Transformation t) { //根据ImageView旋转动画的百分比来显示textview高度，达到动画效果
                    item_expand_textview.setHeight((int) (startHight + tempHight * interpolatedTime));//原始长度+高度差*（从0到1的渐变）即表现为动画效果
                }
            };
            animation.setDuration(durationMillis);
            item_expand_textview.startAnimation(animation);
        }
    }

}